<template>
  <el-dialog width="90%" @opened="openDialog()" v-model="dialogVisible" modal :fullscreen="fullscreen" ref="dialog"
    :before-close="closeDialog" :close-on-click-modal="false" :append-to-body="true" destroy-on-close>
    <template slot="title">
      <div class="avue-crud__dialog__header">
        <span class="el-dialog__title"> 页面设置 </span>
        <span style="margin-right: 40px; color: #909399" class="iconfont actinitem"
          :class="[fullscreen ? 'icon-tuichuquanping' : 'icon-quanping ']"
          @click="fullscreen ? (fullscreen = false) : (fullscreen = true)">
        </span>
      </div>
    </template>
    <el-radio-group v-model="tabRadio">
      <el-radio-button :label="1">{{ label == 1 ? '盒列表设置' : '档案列表设置' }}</el-radio-button>
      <el-radio-button :label="2">{{ label == 1 ? '盒编辑设置' : '档案编辑设置' }}</el-radio-button>
    </el-radio-group>
    <div class="field-filter" v-show="tabRadio == 1">
      <el-transfer v-model="rightData" target-order="unshift" :titles="[label == 1 ? '盒已有字段' : '档案已有字段', '表格排序字段']"
        :data="leftData" @right-check-change="choose" :props="{ key: 'defineId', label: 'fieldTitle' }">
        <div slot-scope="{ option }">
          <div class="default-tranfer-item" @mouseenter="indexKey = option.defineId" @mouseleave="indexKey = null">
            <span>{{ option.fieldTitle }}</span>
            <div v-show="rightData.includes(option.defineId) && indexKey === option.defineId" class="button-group">
              <!--  阻止事件冒泡  -->
              <!-- 自定义数据项，将上、下、底部、顶部的排序功能放在数据项里面 -->

              <em class="el-icon-upload2" @click.stop.prevent="publicMobileMethod('handleTop', option.defineId)"></em>
              <em class="el-icon-top" @click.stop.prevent="publicMobileMethod('handleUp', option.defineId)"></em>
              <em class="el-icon-bottom" @click.stop.prevent="publicMobileMethod('handleDown', option.defineId)"></em>
              <em class="el-icon-download"
                @click.stop.prevent="publicMobileMethod('handleBottom', option.defineId)"></em>
            </div>
          </div>
        </div>
      </el-transfer>
      <span class="dialog-footer" style="float: right; margin-top: 10px">
        <el-button @click="dialogVisible = false" size="mini">取 消</el-button>
        <el-button type="primary" @click="submitfieldName" size="mini">保 存</el-button>
      </span>
    </div>

    <custom-From ref="customFrom" :key="componentKey" v-show="tabRadio == 2" :label="label"></custom-From>
  </el-dialog>
</template>

<script>
import {
  rfidAccountFieldDefinelistBox,
  rfidAccountFieldDefinelistArchive,
  resetDefaultArchiveFieldSort,
  resetDefaultBoxFieldSort,
} from '@/api/sys/field'
import customFrom from './customFrom.vue'

export default {
  components: {
    customFrom,
  },
  props: {
    leftData: {
      type: Array,
      default: () => [],
    },
    label: {
      type: String,
      default: () => '1',
    },
  },
  data() {
    return {
      fullscreen: false,
      tabRadio: '1',
      value: [],
      item: [], // 右侧勾选数据
      indexKey: null, // 高亮显示
      dialogVisible: false,
      rightData: [
        // 选中数据
      ],
      // leftData: [],
      componentKey: 0,
    }
  },
  methods: {
    // 提交数据
    submitfieldName() {
      let nerwarr = this.rightData.map((item, index) => {
        return {
          defineId: item,
          fieldSort: index,
        }
      })
      var url
      if (this.label == 1) {
        url = resetDefaultBoxFieldSort(nerwarr)
      } else {
        url = resetDefaultArchiveFieldSort(nerwarr)
      }
      url
        .then((res) => {
          if (res.data.code == 200) {
            this.$message.success('操作成功')
            // this.dialogVisible = false 取消保存时关闭弹窗 原因操作不方便
            // getbusFieldDefine
          } else {
            this.$message.error(res.data.message)
          }
        })
        .catch((err) => {
          this.$message.error(err)
        })
    },
    closeDialog() {
      this.$emit('destroyed')
      this.dialogVisible = false
    },
    // 打开谈对话框
    openDialog(label) {
      this.componentKey += 1
      this.tabRadio = '1'

      this.$nextTick(() => {
        this.dialogVisible = true
        this.getrfidAccountFieldDefine()
      })
    },
    /**
     * 监听右侧选中
     */
    choose(value) {
      this.item = value
    },
    publicMobileMethod(direction, key) {
      const self = this
      let index
      // 判断是否超出一条

      const item = self.item
      if (item.length === 1 || item.length === 0) {
        self.rightData.forEach((val, indexs) => {
          // 获取需移动数据的下标
          if (val === key) {
            index = indexs
          }
        })
        if (index === 0 && direction !== 'handleBottom' && direction !== 'handleDown') {
          return self.$message('没有上移的空间了')
        }
        if (
          index === self.rightData.length - 1 &&
          direction !== 'handleUp' &&
          direction !== 'handleTop'
        ) {
          return self.$message('没有下移的空间了')
        }
        // 改变的数组
        const changeItem = self.rightData[index]
        // 根据下标在数组中删除该数据
        self.rightData.splice(index, 1)
        // 判断加入数组位置
        if (direction) {
          // 置顶
          direction === 'handleTop' && self.rightData.unshift(changeItem)
          // 置底
          direction === 'handleBottom' && self.rightData.push(changeItem)
          // 上移
          direction === 'handleUp' && self.rightData.splice(index - 1, 0, changeItem)
          // 下移
          direction === 'handleDown' && self.rightData.splice(index + 1, 0, changeItem)
        }
      } else {
        self.$message.error('只能选择一条数据进行上下移动')
      }
    },
    // 获取右侧列表的数据
    getrfidAccountFieldDefine() {
      var url
      if (this.label == 1) {
        url = rfidAccountFieldDefinelistBox()
      } else {
        url = rfidAccountFieldDefinelistArchive()
      }
      url.then((res) => {
        if (res.data.code === 200) {
          this.rightData = res.data.data.map((item) => item.defineId)
        } else {
          this.$message.error(res.data.msg)
        }
      })
    },
  },
  watch: {
    tabRadio(val) {
      this.$refs.customFrom.getbusFieldDefine(this.label)
    },
  },
  mounted() {
    this.$refs.customFrom.getbusFieldDefine(this.label)
  },
}
</script>
<style lang="less">
.field-filter {
  margin: 10px 0 0 0;
  padding-bottom: 50px;

  .determine.el-button--mini {
    background-color: #2a76cd;
    color: #ffffff;
  }

  .el-transfer {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .el-transfer-panel__item {
      margin-right: 0;
    }

    .default-tranfer-item {
      width: 100%;
      display: flex;
      align-items: center;
      justify-content: space-between;

      .button-group {
        font-size: 18px;

        em {
          margin-right: 8px;
        }
      }
    }

    .el-input.el-input--small {
      .el-input__inner {
        border-radius: 4px;
      }
    }

    .el-transfer__buttons {
      padding: 0;
      margin: 0 17px;

      .el-transfer__button {
        display: block;
        margin: 0 0 5px 0;
        padding: 4px 8px;
      }

      .el-button--primary.el-transfer__button {
        display: flex;
        justify-content: center;
        align-items: center;
        background: #2a76cd;
        border-color: #2a76cd;
      }

      .el-button--primary.is-disabled {
        background-color: #a0cfff;
        border-color: #a0cfff;
      }
    }

    .el-checkbox__input.is-indeterminate {
      .el-checkbox__inner {
        background: #2a76cd;
        border-color: #2a76cd;
      }
    }

    .el-transfer-panel {
      width: 49%;
      border-radius: 0;
      height: 500px;
    }

    .el-transfer-panel__body {
      .el-checkbox__label {
        &:hover {
          color: #2a76cd;
        }
      }

      .el-transfer-panel__list {
        height: 500px;
      }
    }

    .el-transfer-panel__footer {
      top: 0;
      left: 61%;
      background-color: transparent;
      display: flex;
      width: 30%;
      justify-content: right;
      border-color: transparent;

      .el-button--text {
        color: #b5b5b5;
        margin-left: 5px;

        .icon-huidaodingbu {
          font-size: 16px;
        }

        em {
          font-size: 14px;
          font-weight: 600;
        }
      }
    }
  }
}

.field-filter .el-transfer .el-transfer-panel__body .el-checkbox__label:hover {
  background: #e6e8ea;
}

.el-transfer-panel__item .el-checkbox .el-checkbox__label {
  padding-left: 10px;
}

//这里注意：我当前将css样式加载全局上，如果单页添加样式需要每个样式前添加 ‘ ' 修饰符

/*  dialog*/
.el-dialog__header {
  padding: 15px 20px 15px;
}

.el-dialog__headerbtn {
  top: 15px;
}

/*dialog header*/
.avue-crud__dialog__header {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  -webkit-box-pack: justify;
  -ms-flex-pack: justify;
  justify-content: space-between;
}
</style>
